#!/usr/bin/python
# -*- coding: UTF-8 -*-
from flask import Blueprint, request, jsonify, json
from flask import render_template
from flask import g

from .compare import get_layui_json
from .db import get_db
from flaskr.CommonUtils.DBUtils import *
from flaskr.CommonUtils.CommonTool import *

import psycopg2
from psycopg2.extras import execute_values
from typing import List


bp = Blueprint("compareSequence", __name__)

@bp.route("/compareSequence")
def compare_sequence():
    task_name = ""
    id_string = ""
    source_column_info = ""
    if request.method == "GET":
        id_string = request.args.get("id")
        print("/detailCompareTask : id_string = " + str(id_string))
        dbutils = DBUtils()
        dbutils.value["dbname"] = "tzqdb"
        dbutils.value["username"] = "tzq"
        dbutils.value["password"] = "1"
        dbutils.value["host"] = "127.0.0.1"
        dbutils.value["port"] = "5432"
        print("str(id_string)=" + str(id_string))

        task_name = dbutils.exec_sql_get_result_from_gauss_or_pg("""
            select task_name from compare_task_t where task_id=""" + str(id_string) + """
            """)

        source_column_info = dbutils.exec_sql_get_result_from_gauss_or_pg("""
            select array_agg(distinct source) as arr,array_length(array_agg(distinct source), 1) as len
              from compare_task_info_v where task_id = """ + str(id_string) + """
            """)
        print("source_column_info = " + str(source_column_info))

    return render_template("compareSequence/compareSequence.html"
                           , source_column_info=source_column_info
                           , id=id_string, task_name=task_name)

@bp.route("/compareSequenceData", methods=['GET'])
def compare_sequence_data():
    sql_string_tmp = ""
    table_name_tmp_string = ""
    sequence_name_tmp_string = ""
    check_result_tmp_string = ""
    if request.method == "GET":
        id_string = request.args.get("id")
        print("/compareSequenceData : id_string = " + str(id_string))

        table_name_string = request.values.get("related_table_name")
        sequence_name_string = request.args.get("sequencename")
        print("/compareSequenceData : table_name = " + str(table_name_string))
        print("/compareSequenceData : sequence_name = " + str(sequence_name_string))

        check_result_string = request.args.get("check_result")
        print("/compareSequenceData : check_result_string = " + str(check_result_string))

        if table_name_string is None:
            table_name_string = ""
        if sequence_name_string is None:
            sequence_name_string = ""
        if table_name_string and len(table_name_string) != 0:
            table_name_tmp_string = (table_name_tmp_string + (
            """
            and (sequence_like_table1 ~* '""" + table_name_string + """' or 
                 sequence_like_table2 ~* '""" + table_name_string + """' or 
                 sequence_like_table3 ~* '""" + table_name_string + """' or 
                 sequence_like_table4 ~* '""" + table_name_string + """' or 
                 sequence_like_table5 ~* '""" + table_name_string + """' or 
                 sequence_like_table6 ~* '""" + table_name_string + """' or 
                 sequence_like_table7 ~* '""" + table_name_string + """')
            """
            ))
            print("/compareSequenceData 2 1 : table_name_tmp_string = " + str(table_name_tmp_string))
        if sequence_name_string and len(sequence_name_string) != 0:
            sequence_name_tmp_string = sequence_name_tmp_string + " and sequencename like '%" + sequence_name_string + "%'"
            print("/compareSequenceData 2 2 : sequence_name_tmp_string = " + str(sequence_name_tmp_string))

        if check_result_string is None:
            check_result_string = ""
        if check_result_string and len(check_result_string) != 0:
            check_result_tmp_string = check_result_tmp_string + " and total_check_result = '" + check_result_string + "'"

        dbutils = DBUtils()
        dbutils.value["dbname"] = "tzqdb"
        dbutils.value["username"] = "tzq"
        dbutils.value["password"] = "1"
        dbutils.value["host"] = "127.0.0.1"
        dbutils.value["port"] = "5432"
        print("str(id_string)=" + str(id_string))
        sql_string_tmp = ("""
select * from get_sequence_info_by_task_id_f(""" + str(id_string) + """)
 where 1=1
               """ + str(table_name_tmp_string) + """
               """ + str(sequence_name_tmp_string) + """
               """ + str(check_result_tmp_string) + """
            """)
    return get_layui_json(sql_string_tmp)


